<?php
/* post type for our team */
$labels = array(
    'name'                  => _x('Our Team', 'post type general name', 'dt'),
    'singular_name'         => _x('Our Team', 'post type singular name', 'dt'),
    'add_new'               => _x('Add New', 'post type new', 'dt'),
    'add_new_item'          => __('Add New Member', 'dt'),
    'edit_item'             => __('Edit', 'dt'),
    'new_item'              => __('New Member', 'dt'),
    'view_item'             => __('View', 'dt'),
    'search_items'          => __('Search', 'dt'),
    'not_found'             =>  __('No Members Found', 'dt'),
    'not_found_in_trash'    => __('No Members Found in Trash', 'dt'), 
    'parent_item_colon'     => '',
    'menu_name'             => 'Our Team'
);

$img = get_template_directory_uri(). '/images/admin_ico_teem.png';

$args = array(
    'labels'                => $labels,
    'public'                => false,
    'publicly_queryable'    => false,
    'show_ui'               => true, 
    'show_in_menu'          => true, 
    'query_var'             => true,
    'rewrite'               => false,
    'capability_type'       => 'post',
    'has_archive'           => false, 
    'hierarchical'          => false,
    'menu_position'         => 31,
    'menu_icon'		        => $img,
    'supports'              => array( 'title', 'thumbnail', 'editor' )
); 
register_post_type( 'dt_our_teem', $args );
			
// metaboxez
add_action( 'add_meta_boxes', 'dt_our_teem_box_register' );

/* Do something with the data entered */
add_action( 'save_post', 'dt_our_teem_box_save' );

function dt_our_teem_box_register() {
    add_meta_box(
        'dt_our_teem',
        __( 'Options', LANGUAGE_ZONE ),
        'dt_our_teem_box',
        'dt_our_teem'
    );
}
			
function dt_our_teem_box( $post ) {
    $boxname = 'our_teem';
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), $boxname. '_nonce' );
    
    $data = get_post_meta( $post->ID, $boxname, true );
    $defaults = array(
        'accupation'    => '',
        'age'           => ''
    );
    $options = wp_parse_args( $data, $defaults );
    ?>
    <p>
        <label for="<?php echo $boxname; ?>_age">
            <?php _e('Age', LANGUAGE_ZONE); ?><br/>
            <textarea name="<?php echo $boxname; ?>_age" id="<?php echo $boxname; ?>_age" style="width: 100%;"><?php echo $options['age']; ?></textarea>
        </label>
    </p>
    <p>
        <label for="<?php echo $boxname; ?>_accupation">
            <?php _e('Accupation', LANGUAGE_ZONE); ?><br/>
            <textarea name="<?php echo $boxname; ?>_accupation" id="<?php echo $boxname; ?>_accupation" style="width: 100%; height: 100px"><?php echo $options['accupation']; ?></textarea>
        </label>
    </p>
    <?php
}
			
function dt_our_teem_box_save( $post_id ) {
    $boxname = 'our_teem';
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
  
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !isset( $_POST[$boxname. '_nonce'] ) || !wp_verify_nonce( $_POST[$boxname. '_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // Check permissions
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
    
    $mydata = null;
    
    if( !empty($_POST[$boxname. '_accupation']) ) {
        $mydata['accupation'] = $_POST[$boxname. '_accupation'];
    }
    
    if( !empty($_POST[$boxname. '_age']) ) {
        $mydata['age'] = $_POST[$boxname. '_age'];
    }
    
    update_post_meta( $post_id, $boxname, $mydata );
}